/**
 * LonixFloat
 *
 * 增强型浮动控件
 *
 * @Date 2017-06-05 17:38:37
 * @Auther TomSean (杨仲青)
 */
$(document).bind('click', function(event) {
    event.stopPropagation();
    event.cancelBubble = true;
    $('.lonix.float:visible').stop(true);
    $('.lonix.float:visible').fadeUp();
});

var LonixFloat = function ($) {
    this.$ = $;
}
LonixFloat.prototype = {
    // 初始化LonixFloat
    init : function () {

        // 将事件绑定到LonixFloat对象
        this.bindEvent();

        //阻止冒泡事件
        this.$.bind('click', function (event) {
            event.stopPropagation();
            event.cancelBubble = true;
        });
    },

    // 装载数据到floatView (JSON数据, 装载到哪个父节点)
    deployFloat : function (json, parent) {

    },
    // 绑定事件函数
    bindEvent : function () {
        var $this = this;
        $this.$.find('.lonix.search-input').bind('keyup', function(event) {
            var $search = $(this);
            var inputValue = ($search.val() + '');
            $this.$.find('.imp').each(function(inx, itm) {
                var span = $(itm).html();
                var parentHtml = $(itm).parent().html().replace('<span class="imp">'+span+'</span>', span);
                $(itm).parent().html(parentHtml);
            });
            if (inputValue == '') {
                $this.$.find('li[treenode]').show();
            } else {
                $this.$.find('li[treenode]').hide();
                var filterNodes = $this.ztree.getNodesByParamFuzzy($this.searchField, inputValue, null);
                for (var i = 0; i < filterNodes.length; i++) {
                    var name = filterNodes[i][$this.searchField];
                    var before = name.substring(0, name.indexOf('>') + 1);
                    var after = name.substring(name.indexOf('>')+1, name.length);
                    after = after.replace($search.val(), '<span class="imp">' + $search.val() + '</span>');
                    $this.$.find('#' + filterNodes[i].tId + '_span').html(after);
                    $this.$.find('#' + filterNodes[i].tId + '_span').parents('li').show();
                    $this.$.find('#' + filterNodes[i].tId + '_ul li').show();
                }
            }
            if ($this.$.find('.float-body li[treenode]:visible').length < 1) {
                $this.$.find('.lonix.float-prompt').html('<p style="margin:0;">没有“'+$search.val()+'”的信息<p>');
                $this.$.find('.lonix.float-prompt').show();
            } else {
                $this.$.find('.lonix.float-prompt').hide();
            }
        });

        $this.$.find('.lonix.float-toolbar .float-add').bind('click', function(event) {
            alert('添加');
        });

        $this.$.find('.lonix.float-toolbar .float-remove').bind('click', function(event) {
            if (confirm('确认删除所有选中节点吗？')) {
                var filterNodes = $this.ztree.getCheckedNodes(true);
                for (var i = 0; i < filterNodes.length; i++) {
                    $this.ztree.removeNode(filterNodes[i]);
                }
            }
        });

        $this.$.find('.lonix.float-footer .primary').bind('click', function(event) {
            var nodes = $this.getFloatSelectedValue();
            $this.primary(nodes);
        });

        $this.$.find('.lonix.float-footer .cancel').bind('click', function(event) {
            var nodes = $this.ztree.getSelectedNodes();
            for (var i = 0; i < nodes.length; i++) {
                $this.ztree.cancelSelectedNode(nodes[i]);
            }
            $this.cancel(nodes);
            $this.$.fadeUp();
        });
    },
    getFloatSelectedValue : function () {
        var $this = this;
        if (isEmpty($this.ztree)) return {};
        var nodes;
        if ($this.ztree.setting.check.enable) {
            nodes = $this.ztree.getCheckedNodes(true);
        } else {
            nodes = $this.ztree.getSelectedNodes();
        }
        return nodes;
    },
    setLocation : function (x, y) {
        var $this = this;
        $this.$.css({'top':y, 'left':x});
        return $this;
    },
    show : function (ms) {
        ms = ms || 350;
        this.$.stop(true);
        this.$.fadeDown();
        return this;
    },
    close : function () {

    }
}

$.fn.extend({
    lonixFloat : function (option) {
        option = option || {};
        var value;
        var lonixFloat = new LonixFloat(this);

        if (undefined != option.target) {
            if (typeof(option.target) === '[object String]') {
                option.target = $(option.target);
            }
            option.target.unbind('click').bind('click', function(event) {
                event.stopPropagation();
                event.cancelBubble = true;
                var x = $(this).offset().left;
                if (undefined != option.float) {
                    if ('right' == option.float) {
                        var targetWidth = $(this).width();
                        var floatWidth = lonixFloat.$.width();
                        x = floatWidth > targetWidth ? x - (floatWidth - targetWidth) : x;
                    }
                }
                lonixFloat.setLocation(x, $(this).offset().top + $(this)[0].clientHeight + 5).show();
            });
            value = $(option.target).html() || $(option.target).val();
        }
        if (undefined != option.source) {
            var setting = option.setting || {
                    view : {
                        showLine : true,
                        selectedMulti: true,
                        showIcon: false
                    },
                    check: {
                        enable: true,
                        chkboxType : { 'Y':'s', 'N':'s'}
                    },
                    data : {
                        simpleData : {
                            enable : true,
                            idKey : 'id',
                            pIdKey : 'pId'
                        }
                    }
                }
            lonixFloat.searchField = option.searchField || 'name';
            lonixFloat.ztree = $.fn.zTree.init( $(this).find('.ztree'), setting, option.source );
            if (isEmpty(lonixFloat.ztree.setting.check)) {
                lonixFloat.ztree.setting.check = {enable:false, chkboxType:{'Y':'s', 'N':'s'}};
            }
            lonixFloat.ztree.expandAll(true);
            var curNode = lonixFloat.ztree.getNodeByParam(lonixFloat.ztree.setting.data.simpleData.idKey, value, null);
            lonixFloat.ztree.selectNode(curNode);
            if (!isEmpty(curNode)) {
                if (option.target[0].targetName == 'input' || option.target[0].targetName == 'select' || option.target[0].targetName == 'textarea') {
                    option.target.val(curNode['name']);
                } else {
                    option.target.html(curNode['name']);
                }
            }
        }
        if (undefined != option.primary && typeof(option.primary) === 'function') {
            lonixFloat.primary = option.primary;
        }
        if (undefined != option.cancel && typeof (option.cancel) === 'function') {
            lonixFloat.cancel = option.cancel;
        }
        lonixFloat.init();
        return lonixFloat;
    },
    fadeDown : function (ms) {
        var y = $(this).css('top');
        y = parseInt(y);
        var $y = y - 50;
        $(this).css({'position':'fixed', 'top':y, 'display':'block', 'opacity':1});
    },
    fadeUp : function (ms) {
        var y = $(this).css('top');
        $(this).css({'position':'fixed', 'top':y, 'display':'none', 'opacity':0});
    }
});